﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows;
using wms.Common;

namespace wms.Pages
{
    /// <summary>
    /// GoodsInput.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsInput : Window
    {
        private Dictionary<string, object> _data;

        public GoodsInput(Dictionary<string, object> data)
        {
            this.Data = data;
            ChangeLanguage.changePage(Variable.LanguageSeesion.ToString() );
            InitializeComponent();
            this.DataContext = this.Data;
        }

        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string sql = "update t_goods set code = @code,name = @name,weight = @weight,type = @type,size = @size,value = @value where id = @id";

                int? id = Utils.ParseInt(this.Data["id"]);
                string name = Utils.Parse(this.Data["name"]);
                string code = Utils.Parse(this.Data["code"]);
                float? weight = Utils.ParseFloat(this.Data["weight"]);
                int? quantity = Utils.ParseInt(this.Data["quantity"]);
                string type = Utils.Parse(this.Data["type"]);
                string size = Utils.Parse(this.Data["size"]);
                float? value = Utils.ParseFloat(this.Data["value"]);

                if (string.IsNullOrWhiteSpace(name))
                {
                    MessageBox.Show("物品名称不能为空");
                    return;
                }
                if (quantity == null)
                {
                    quantity = 0;
                }
                if (quantity < 0)
                {
                    MessageBox.Show("物品数量不能小于0");
                    return;
                }
                if (value == null || value < 0)
                {
                    MessageBox.Show("声明价值无效");
                    return;
                }
                if (weight != null && weight < 0)
                {
                    MessageBox.Show("重量不能小于0");
                    return;
                }
                if (string.IsNullOrWhiteSpace(code))
                {
                    MessageBox.Show("物品编码不能为空");
                    return;
                }
                int count = DBHelper.GetCount("select code from t_goods where code = @code and id != @id", new SqlParameter[] { new SqlParameter("@code", code), new SqlParameter("@id", id) });
                if (count > 0)
                {
                    MessageBox.Show("物品编码已经存在");
                    return;
                }

                int result = DBHelper.ExcuteSQL(sql, new Dictionary<string, object> { { "@code", code }, { "@name", name }, { "@quantity", quantity }, { "@weight", weight }, { "@type", type }, { "@size", size }, { "@value", value }, { "@id", id } });
                if (result == 1)
                {
                    MessageBox.Show("保存成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败\n" + ex.Message);
            }
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        public Dictionary<string, object> Data
        {
            get
            {
                return _data;
            }

            set
            {
                _data = value;
            }
        }

        private void btn_select_Click(object sender, RoutedEventArgs e)
        {
            Type win = new Type();
            bool? dialogResult = win.ShowDialog();
            if (dialogResult != null && dialogResult == true)
            {
                this.txt_type.Text = win.Value.Id + ")" + win.Value.Name;
                this.Data["type"] = this.txt_type.Text;
            }
        }
    }
}
